package com.coder.oj.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.oj.common.core.JsonPage;
import com.coder.oj.common.entity.Answer;
import com.coder.oj.common.entity.Discuss;
import com.coder.oj.common.mapper.AnswerMapper;
import com.coder.oj.common.mapper.DiscussMapper;
import com.coder.oj.system.dto.AnswerDto;
import com.coder.oj.system.mapstruct.AnswerMapstruct;
import com.coder.oj.system.query.AnswerQuery;
import com.coder.oj.system.utils.CheckUserUtils;
import com.coder.oj.system.vo.AnswerVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AnswerService implements Serializable {

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private AnswerMapstruct answerMapstruct;

    @Autowired
    private DiscussMapper discussMapper;

    /**
     * 分页查询
     * @author Bosen 2023/1/10 10:38
     **/
    public JsonPage<AnswerVo> getAnswerPage(AnswerQuery params) {
        LambdaQueryWrapper<Answer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(params.getTitle()), Answer::getTitle, params.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(params.getContent()), Answer::getContent, params.getContent());
        queryWrapper.eq(ObjectUtils.isNotEmpty(params.getStatus()), Answer::getStatus, params.getStatus());
        queryWrapper.eq(ObjectUtils.isNotEmpty(params.getProblemId()), Answer::getProblemId, params.getProblemId());
        IPage<Answer> page = answerMapper.selectPage(new Page<>(params.getPage(), params.getPageSize()), queryWrapper);
        List<AnswerVo> collect = page.getRecords().stream().map(answerMapstruct::toVO).collect(Collectors.toList());
        return new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
    }

    /**
     * 删除题解
     * @author Bosen 2023/1/10 10:40
     **/
    public void deleteAnswerById(Long id) {
        Answer answer = answerMapper.selectById(id);
        CheckUserUtils.checkCreateBy(answer);
        // 删除题解对应的评论
        LambdaQueryWrapper<Discuss> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Discuss::getAnswerId, id);
        discussMapper.delete(queryWrapper);
        // 删除题解
        answerMapper.deleteById(id);
    }

    /**
     * 修改题解
     * @author Bosen 2023/1/10 10:44
     **/
    public void updateAnswer(AnswerDto params) {
        Answer answer = answerMapper.selectById(params.getId());
        answer.setTitle(params.getTitle());
        answer.setContent(params.getContent());
        CheckUserUtils.checkCreateBy(answer);
        answerMapper.updateById(answer);
    }
}
